        
        dojo.require('dojo.parser');
        dojo.require('dojo.data.ItemFileReadStore');
        dojo.require('dojo.data.ItemFileWriteStore');
        dojo.require("dojox.grid.compat._grid.edit");
        dojo.require('dojox.grid.DataGrid');
        dojo.require('dijit.form.Button');
        dojo.require('dijit.Dialog');

        var catalogStore = new dojo.data.ItemFileWriteStore({data:catalogArray});	
        var cartStore = new dojo.data.ItemFileWriteStore({data:cartArray});

		function filterCatalogItems()
		{
			var _filterTextBox = document.getElementById('filtertext');
			var _filterText;
			if(_filterTextBox.value == '')
				_filterText = '*';
			else
				_filterText = '*'+_filterTextBox.value+'*';
			var query = { id:'*', name:_filterText };
			catalogGrid.setQuery(query);
		}
		
		function resetCatalogItemsFilter()
		{
			var _filterTextBox = document.getElementById('filtertext');
			var query = { id:'*' };
			_filterTextBox.value = '';
			catalogGrid.setQuery(query);

		}
		
		function priceFormatter(price) {
			return '$'+price;	
		}
		
		function setCartItemsValue(str)
		{
			var carts =	document.getElementById('cartItems');
			carts.value = str;
		}
		
		function updateCartItemsField()
		{
			var carts =	document.getElementById('cartItems');
			carts.value = collectItemsFromCart();	
		}
		
        function itemHasChanged(item, attribute, oldValue, newValue)
        {
            if( oldValue == newValue ) return;
            if( attribute == 'total' ) return;
            var itemPrice = cartGrid.store.getValue( item, 'price' );
            var itemQty = cartGrid.store.getValue( item, 'qty' );
            var newTotal = itemPrice * itemQty;
            cartGrid.store.setValue( item, 'total', newTotal );
			updateCartItemsField();
        }

        function gridDblClkHandler(e)
        {
            var cartItems = getSelectedCartItems();
            var hasNonEditables = false;
			/*
            for( var i=0; i < cartItems.length; i++ )
            {
                var selectedItem = cartItems[i];
                var editable = cartGrid.store.getValues(selectedItem, 'type');
                if(editable != "ALACARTE")
                    hasNonEditables = true;
            }
			*/
            if(hasNonEditables)
            {
                cartGrid.edit.cancel();
                alert('You have some non-editable items in your selection.  Please deselect these before trying to edit your shopping cart.');
                return false;
            }
            
            return true;
        }
        
		function addPicturedItemToCart( priceListId ) {
			// query catalog grid to get back item
			// add as a new item to cart	
			var gotPicturedItem = function(items, request){
			    for (var i = 0; i < items.length; i++){
			        var item = items[i];
	                var id = catalogGrid.store.getValues(item, 'id');
	                var name = catalogGrid.store.getValues(item, 'name');
					
					var didx = idx++;
					
	                var id = catalogGrid.store.getValues(item, 'id');
	                var name = catalogGrid.store.getValues(item, 'name');
	                var cl = catalogGrid.store.getValues(item, 'cl');
	                var price = catalogGrid.store.getValues(item, 'price');
					var category = catalogGrid.store.getValues(item, 'category');
					var type = 'ALACARTE';				
	                var qty = 1;
	                var total = qty*price;
	
	                cartGrid.store.newItem({ idx: didx, id: id, cl:cl, name:name, category:category, price:price, qty:qty, total:total, type:'ALACARTE' });
			    }
			}
			var request = catalogStore.fetch({query: {id:priceListId}, onComplete: gotPicturedItem});
			
		}
		
        function getSelectedCatalogItems()
        {
            var items = catalogGrid.selection.getSelected();
            return items;
        }

        function getSelectedCartItems()
        {
            var items = cartGrid.selection.getSelected();
            return items;
        }

        function addSelectedItemToCart()
        {
            var selectedItems = getSelectedCatalogItems();
            if(selectedItems.length<1)
            {
                alert('Please select an item you would like to add.');
                return;
            }

            for( var i=0; i < selectedItems.length; i++ )
            {
                var selectedItem = selectedItems[i];
				var didx = idx++;
				
                var id = catalogGrid.store.getValues(selectedItem, 'id');
                var name = catalogGrid.store.getValues(selectedItem, 'name');
                var cl = catalogGrid.store.getValues(selectedItem, 'cl');
                var price = catalogGrid.store.getValues(selectedItem, 'price');
				var category = catalogGrid.store.getValues(selectedItem, 'category');
				var type = 'ALACARTE';				
                var qty = 1;
                var total = qty*price;

                cartGrid.store.newItem({ idx: didx, id: id, cl:cl, name:name, category:category, price:price, qty:qty, total:total, type:'ALACARTE' });
            }                
			updateCartItemsField();
        }

        function removeSelectedItemFromCart()
        {
            var selectedItems = getSelectedCartItems();
            if(selectedItems.length<1)
            {
                alert('Please select an item you would like to remove.');
                return;
            }

            // in here, check to see if selection contains any
            // non-removable items.  if it does, alert user and book.
            var hasNonEditables = false;
			/*
            for( var i=0; i < selectedItems.length; i++ )
            {
                var selectedItem = selectedItems[i];
                var editable = cartGrid.store.getValues(selectedItem, 'type');
                if(editable != "ALACARTE")
                    hasNonEditables = true;
            }
			*/
            if(hasNonEditables)
            {
                alert('You have some non-editable items in your selection.  Please deselect these before trying to remove them from the shopping cart.');
                return;
            }

            var selectedItem = selectedItems[0];
            cartGrid.store.deleteItem(selectedItem);
			updateCartItemsField();
        }

        function collectItemsFromCart()
        {
            // can also just make this an XML string and send it...
            var xmlStr = '<cartItems>';
            for( var i=0; i < cartArray.items.length; i++ )
            {
                xmlStr = xmlStr + '<cartItem>';
				xmlStr = xmlStr + '<type>' + cartArray.items[i].type + '</type>';
				xmlStr = xmlStr + '<category>' + cartArray.items[i].category + '</category>';
                xmlStr = xmlStr + '<price_list_id>' + cartArray.items[i].id + '</price_list_id>';
                xmlStr = xmlStr + '<CL>' + cartArray.items[i].cl + '</CL>';
                xmlStr = xmlStr + '<name>' + cartArray.items[i].name + '</name>';
                xmlStr = xmlStr + '<price>' + cartArray.items[i].price + '</price>';
                xmlStr = xmlStr + '<qty>' + cartArray.items[i].qty + '</qty>';
				xmlStr = xmlStr + '<total>' + cartArray.items[i].total + '</total>';
                xmlStr = xmlStr + '</cartItem>';
            }
            var xmlStr = xmlStr + '</cartItems>';
            return xmlStr;
        }

        dojo.addOnLoad( function() {

        catalogGrid = new dojox.grid.DataGrid({
            query: catalogQuery,
            store: catalogStore,
            structure: catalogLayout,
            rowsPerPage: 5,
            selectionMode: 'single'
            }, 'dCatalogGrid');

        cartGrid = new dojox.grid.DataGrid({
            query: cartQuery,
            store: cartStore,
            structure: cartLayout,
            rowsPerPage: 5,
            selectionMode: 'single'
            }, 'dCartGrid');

            catalogGrid.startup();
			console.log('about to start up cart grid');
            cartGrid.startup();
			console.log('started up cart grid');

			updateCartItemsField();
						
            dojo.connect( cartGrid, "onCellDblClick", gridDblClkHandler );
            dojo.connect( cartGrid.store, "onSet", itemHasChanged );
});
